package jp.syuriken.snsw.terenaibot.format.function;

import jp.syuriken.snsw.terenaibot.format.CompileArg;
import jp.syuriken.snsw.terenaibot.format.FormatSyntaxException;
import jp.syuriken.snsw.terenaibot.format.PrecompiledSyntaxGroup;

/**
 * TODO snsoftware
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $ */
public class IntDivideFunction extends AbstractFormatFunction {
	
	@Override
	public String getReturnValue(PrecompiledSyntaxGroup[] args, CompileArg compileArg) throws FormatSyntaxException {
		checkArgLength(args, compileArg, "/", 2);
		int a;
		try {
			a = Integer.parseInt(args[0].format(compileArg));
		} catch (NumberFormatException e) {
			throw new FormatSyntaxException(String.format("Function</>: arg%d is not integer value: %s (formatted:%s)",
					0, args[0].toString(), args[0].format(compileArg)));
		}
		int b;
		try {
			b = Integer.parseInt(args[1].format(compileArg));
		} catch (NumberFormatException e) {
			throw new FormatSyntaxException(String.format("Function</>: arg%d is not integer value: %s (formatted:%s)",
					1, args[1].toString(), args[1].format(compileArg)));
		}
		if (b == 0) {
			throw new FormatSyntaxException(String.format("Function</>: arg2 is zero: %s", args[1].toString()));
		} else {
			return String.valueOf(a / b);
		}
	}
}
